<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of message
 *
 * @author Andi
 */
include_once("database.php");

class message extends database {
    //put your code here

    function  __construct() {
        $this->connect();
    }


    function parsing($email){
        // define comma ',' -separated list
        // decompose string into array
        // using comma as delimiter
        $email = explode(",", $email);
        return $email;
    }

    function addMemberGroup($target){
        $sql = "SELECT MAX(group_id) AS 'max' FROM group_member";
        $result = mysql_query($sql);
        $result = mysql_fetch_array($result);
        $group_id = $result['max'] + 1;
        $target = $this->parsing($target);
        foreach($target as $i){
            $sql = "INSERT INTO group_member (group_id,user_name) VALUES('$group_id','$i')";
            mysql_query($sql);
        }
        return $group_id;
    }

    function sentMsg($userName,$userNameTarget,$msgSubject,$msgBody,$replyOfMsgID){
        $groupID = $this->addMemberGroup($userNameTarget);
        $date_now = date('Y-m-d');
        $time_now = strftime('%X');
        $sql = "INSERT INTO msg (user_name,group_id,msg_subject,msg_body,post_date,post_time,reply) VALUE ('$userName','$groupID','$msgSubject','$msgBody', '$date_now', '$time_now','$replyOfMsgID')";
        mysql_query($sql);
    }

    function showMsg($userName){
        $sql = "SELECT msg.user_name AS 'from', msg.group_id, msg_id, group_member.user_name AS 'to', msg.msg_subject AS 'subject', msg.msg_body AS 'body',post_date,post_time FROM msg,group_member WHERE group_member.group_id = msg.group_id AND group_member.user_name = '$userName' AND group_member.delete = 'no' AND reply = '0' ORDER BY 'msg_id' DESC";
        $result = mysql_query($sql);
        return $result;
    }

    function showReplyMsg($userName,$replyOfMsgID){
        $sql = "SELECT msg.user_name AS 'from', msg.group_id, msg_id, group_member.user_name AS 'to', msg.msg_subject AS 'subject', msg.msg_body AS 'body',post_date,post_time FROM msg,group_member WHERE group_member.group_id = msg.group_id AND group_member.user_name = '$userName' AND group_member.delete = 'no' AND reply = '$replyOfMsgID'";
        $result = mysql_query($sql);
        return $result;
    }

    function showMsgByMsgID($msgID){
        $sql = "SELECT * FROM msg WHERE msg_id = '$msgID'";
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result);
        return $row;
    }

    function showGroup($groupID){
        $sql = "SELECT * FROM group_member WHERE group_id = '$groupID'";
        $result = mysql_query($sql);
        $to = "";
        while ($row = mysql_fetch_array($result)){
            $to = $to . $row['user_name'] . ',';
        }
        return $to;
    }

    function delMsg($groupId,$userName){
        $sql = "UPDATE group_member SET group_member.delete = 'yes' WHERE group_id = '$groupId' AND user_name = '$userName'";
        mysql_query($sql);
    }

    function getNumRowMsg($userName){
        $sql = "SELECT * FROM `group_member`,msg where `delete` = 'no' and msg.group_id = group_member.group_id and reply = '0' and group_member.user_name = '$userName'";
        $result = mysql_query($sql);
        $numRow = mysql_num_rows($result);
        return $numRow;
    }

    function getLastPage($userName,$numMsgPerPage){
        $numRow = $this->getNumRowMsg($userName);
        $lastPage = ceil($numRow/$numMsgPerPage);
        return $lastPage;
    }

    function showMsgByPagination($userName, $numMsgPerPage, $pageno){
        $limit = ' LIMIT ' .($pageno - 1) * $numMsgPerPage .' ,' .$numMsgPerPage;
        $sql = "SELECT msg.user_name AS 'from', msg.group_id, msg_id, group_member.user_name AS 'to', msg.msg_subject AS 'subject', msg.msg_body AS 'body',post_date,post_time FROM msg,group_member WHERE group_member.group_id = msg.group_id AND group_member.user_name = '$userName' AND group_member.delete = 'no' AND reply = '0' ORDER BY `msg`.`msg_id` DESC $limit ";
        $result = mysql_query($sql);
        return  $result;
    }

}
?>
